/*
 * @version: v1.0
 * @Author: ItLy
 * @Date: 2021-03-03 21:57:45
 * @LastEditors: ItLy
 * @LastEditTime: 2021-03-13 17:12:03
 */

export function strToDate(DateStr)  {   
    var d = new Date(DateStr);
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; 
    var curr_year = d.getFullYear();
    String(curr_month).length < 2 ? (curr_month = "0" + curr_month): curr_month;
    String(curr_date).length < 2 ? (curr_date = "0" + curr_date): curr_date;
    var yyyyMMdd = curr_year + "-" + curr_month +"-"+ curr_date;
    return yyyyMMdd;
} 
export function formatDate(dateTime = null) {
    dateTime = Number(new Date(dateTime));
    // 如果dateTime长度为10或者13，则为秒和毫秒的时间戳，如果超过13位，则为其他的时间格式
    if (dateTime.toString().length == 10) dateTime *= 1000;
    let timestamp = +new Date(Number(dateTime));

    let timer = (Number(new Date()) - timestamp) / 1000;
    // 如果小于5分钟,则返回"刚刚",其他以此类推
    let tips = '';
    switch (true) {
        case timer < 300:
            tips = '刚刚';
            break;
        case timer >= 300 && timer < 3600:
            tips = parseInt(timer / 60) + '分钟前';
            break;
        case timer >= 3600 && timer < 86400:
            tips = parseInt(timer / 3600) + '小时前';
            break;
        case timer >= 86400 && timer < 2592000:
            tips = parseInt(timer / 86400) + '天前';
            break;
        default:
            if (timer >= 2592000 && timer < 365 * 86400) {
                tips = parseInt(timer / (86400 * 30)) + '个月前';
            } else {
                tips = parseInt(timer / (86400 * 365)) + '年前';
            }
    }
    return tips;

}

// import Vue from 'vue'
// let util = {
//     install(Vue){
//         Vue.prototype.util = {
//             formatDate: formatDate,
//             strToDate: strToDate
//         }
//     }
// } 
// Vue.use(util)

export default {
    // apiUrl,
    strToDate,
    formatDate
} 




